﻿namespace NJQuery {
	using System;
	using System.ComponentModel;
	using System.Diagnostics.CodeAnalysis;

	/// <summary>
	/// Server control used to define the properties of a <see href="http://docs.jquery.com/UI/Effects/Fold">jQuery UI Fold effect</see>, which folds the element like a piece of paper.
	/// </summary>
	public class FoldEffect : EffectBase {


		#region Constants: Property defaults

		private const bool defaultHorizFirst = false;
		private const int defaultSize = 15;
		private const FoldMode defaultMode = FoldMode.Hide;

		#endregion


		#region Constructors

		/// <summary>
		/// Initializes a new instance of the <see cref="FoldEffect"/> class.
		/// </summary>
		public FoldEffect() : base("fold") { }

		#endregion


		#region Properties: Initialization options

		/// <summary>
		/// Whether to fold horizontally first or not.
		/// This property maps directly to the "horizFirst" option when the jQuery UI effect is initialized.
		/// </summary>
		[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Horiz", Justification = "Using jQuery UI naming, abbreviations and all, for better or worse.")]
		public virtual bool HorizFirst {
			get { return ViewState["HorizFirst"] as bool? ?? defaultHorizFirst; }
			set { ViewState["HorizFirst"] = value; }
		}

		/// <summary>
		/// Size to be folded to.
		/// This property maps directly to the "size" option when the jQuery UI effect is initialized.
		/// </summary>
		//TODO: Figure out reasonable range
		public virtual int Size {
			get { return ViewState["Size"] as int? ?? defaultSize; }
			set { ViewState["Size"] = value; }
		}

		/// <summary>
		/// The mode of the effect.
		/// This property maps directly to the "mode" option when the jQuery UI effect is initialized.
		/// </summary>
		/// <exception cref="ArgumentException">Value not defined.</exception>
		public virtual FoldMode Mode {
			get { return ViewState["Mode"] as FoldMode? ?? defaultMode; }
			set {
				if(!Enum.IsDefined(typeof(FoldMode), value)) throw new ArgumentException("Value not defined.", "value");
				ViewState["Mode"] = value;
			}
		}

		#endregion


		#region Methods: Rendering hooks

		/// <summary>
		/// Allows a jQuery server control to reference scripts and stylesheets necessary to render their client markup and widgets.
		/// </summary>
		/// <param name="manager">The <see cref="Manager"/> used to handle style and script referencing in addition to unified generation of load and ready scripts.</param>
		protected override void Register(Manager manager) {
			base.Register(manager);

			manager.RegisterClientScript(ClientScript.UIFoldEffect);
		}

		/// <summary>
		/// Returns a <see cref="PropertyCollection"/> containing option values specific to the effect.
		/// This method is used primarily by control developers.
		/// </summary>
		/// <returns>A <see cref="PropertyCollection"/> containing the effect's option values.</returns>
		protected override PropertyCollection CreateOptionProperties() {
			PropertyCollection properties = base.CreateOptionProperties();

			bool horizFirst = HorizFirst;
			if(horizFirst != defaultHorizFirst) properties.AddValue("horizFirst", horizFirst);

			int size = Size;
			if(size != defaultSize) properties.AddValue("size", size);

			FoldMode mode = Mode;
			if(mode != defaultMode) properties.AddEnumValue("mode", mode);

			return properties;
		}

		#endregion


	}

}